package services.common;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import models.common.bean.AnXinSignBean;
import models.common.bean.InformationAssuranceBean;
import models.common.entity.t_information_assurance;
import models.common.entity.t_pact;
import models.common.entity.t_shareholder_sign;
import models.common.entity.t_template_pact;
import models.common.entity.t_user;
import models.common.entity.t_user_credit;
import services.base.BaseService;

import common.enums.PactType;
import common.utils.AnXinSignUtil;
import common.utils.DateUtil;
import common.utils.Factory;
import common.utils.GenerateContract;
import common.utils.LoggerUtil;
import common.utils.PageBean;
import common.utils.ResultInfo;
import common.utils.StrUtil;

import daos.common.InformationAssuranceDao;

/**
 * 信用额度申请service
 * 
 * @author fanghanyong
 * @version 2018-01-18 上午11:00:00
 */
public class InformationAssuranceService extends
		BaseService<t_information_assurance> {

	protected InformationAssuranceDao informationAssuranceDao = Factory.getDao(InformationAssuranceDao.class);
	public InformationAssuranceService() {
		informationAssuranceDao = Factory.getDao(InformationAssuranceDao.class);
		super.basedao = this.informationAssuranceDao;
	}
	protected static UserService userService = Factory.getService(UserService.class);
	protected static ShareholderSignService shareholderSignService = Factory.getService(ShareholderSignService.class);
	protected static PactService pactService=Factory.getService(PactService.class);
	
	/**
	 * 更新数据
	 * 
	 * @param t
	 * @return
	 * @author fanghanyong
	 * @version 2018-01-18
	 */
	public boolean save(t_information_assurance t) {
		return informationAssuranceDao.save(t);
	}

	/**
	 * 查询保证信息表信息
	 * 
	 * @param user_id
	 *            申请人id
	 * @param lender_id
	 *            放款人id
	 * @param credit_limit_id
	 *            授信申请表id
	 * @param contract_id
	 *            合同编号
	 * @return
	 * @author fanghanyong
	 * @version 2018-1-18
	 */
	public t_information_assurance findByUserId(long user_id, long lender_id,
			long credit_limit_id, String contract_id) {
		return findByColumn(
				" user_id = ? AND lender_id = ? AND credit_limit_id = ? ",
				user_id, lender_id, credit_limit_id);
	}

	/**
	 * 分页查询 信用额度申请记录
	 * 
	 * @param currPage
	 * @param pageSize
	 * @param supervisorId
	 *            放款人ID
	 * @param applyName
	 *            申请人姓名
	 * @param stutas
	 *            授信申请状态
	 * @param startDate
	 *            申请起始时间
	 * @param endDate
	 *            申请结束时间
	 * @param applyId
	 *            申请人ID
	 * @param lenderName
	 *            放款人姓名
	 * @return
	 * @author fanghanyong
	 * @param orderValue 
	 * @createDate 2018年01月11日
	 */
	public PageBean<InformationAssuranceBean> pageOfInfoAss(int currPage,int pageSize, long supervisorId, String applyName, int status,
			String startDate, String endDate, String lenderName, long applyId, int exports, int orderType ,int orderValue) {

		return informationAssuranceDao.pageOfInfoAss(currPage, pageSize,
				supervisorId, applyName, status, startDate, endDate,
				lenderName, applyId, exports,orderType,orderValue);
	}

	/**
	 * 更新签字状态
	 * 
	 * @param id
	 *            保证人信息表id
	 * @param dirStatus
	 *            保证人信息表更新的签字状态
	 * @param srcStatus
	 *            保证人信息表原签字状态
	 * @return
	 * @author fanghanyong
	 * @version 2018-1-23
	 */
	public boolean updateInfoAssStatus(long id, int srcStatus, int dirStatus) {
		return informationAssuranceDao.updateInfoAssStatus(id, srcStatus,
				dirStatus);
	}
	
	/**
	 * 新增承若函合同数据
	 * @param id 商票额度id
	 * @param user_id  当前用户的id
	 * @return t_pact 合同实体
	 * @author xiaojun
	 *@version 2017-5-22
	 */
	public  ResultInfo savePact(long id,String lender_name,String party_a,String pact_no,List<AnXinSignBean> list,String key_name){
		ResultInfo result = new ResultInfo();
		
		Map<String,String> contentParam=new HashMap<String,String>();
//		甲方 借款人
		contentParam.put("party_a",party_a);
//		乙方方 保证人(股东)
		contentParam.put("lender_name", lender_name);
		contentParam.put("key_name", key_name);
		//获取模板
		t_template_pact temp = pactService.findByType(PactType.JOINT_LIABILITY);
		if (null == temp) {
			result.code = -1;
			result.msg = "合同模板获取失败";
			return result;
		}
		// 合同编号:pact_no
		contentParam.put("pact_no", pact_no);
		// 合同名称:pact_name
		contentParam.put("pact_name", temp.name);
		//显示隐藏
		contentParam.put("none", "");
		// 签署时间
		String now = DateUtil.dateToString(new Date(), "yyyy年MM月dd日");
		contentParam.put("yyyy年MM月dd日", now);
		temp.content = StrUtil.replaceByMap(temp.content,  contentParam);
		t_pact pact = pactService.findByColumn(" type=? and pid=?", PactType.JOINT_LIABILITY.code,id);
		if(pact !=null){
			result.code = 785;
			result.msg = "合同添加成功";
			result.obj = pact;
			return result;
		}
		pact = new t_pact();
		pact.time = new Date();
		pact.pid =  id;
		pact.setType(PactType.JOINT_LIABILITY);
		pact.content = temp.content;
		pact.pact_no = pact_no;
		pact.image_url = "";
		
		result = pactService.exportPact(pact.image_url,pact.content, false);
	 	File file = (File)result.obj;
	 	result = AnXinSignUtil.anXinSign(file,"股东连带责任协议书",list,0,"",false);
	 	if(result.code < 1){
	 		LoggerUtil.error(true, result.msg);
			return result;
	 		
	 	 }
	 	pact.contract_no = result.obj.toString();
		result = pactService.createPact(pact);
		if (result.code < 1) {
			return result;
		}
		
		return result;
	}
	

	/**
	 * 总额度查询 信用额度申请记录
	 * 
	 * @param supervisorId
	 *            放款人ID
	 * @param applyName
	 *            申请人姓名
	 * @param stutas
	 *            授信申请状态
	 * @param startDate
	 *            申请起始时间
	 * @param endDate
	 *            申请结束时间
	 * @param applyId
	 *            申请人ID
	 * @param lenderName
	 *            放款人姓名
	 * @return
	 * @author fanghanyong
	 * @createDate 2018年01月11日
	 */
	public Map<String, Object> totalInfoAss(long supervisorId,
			String applyName, int status, String startDate, String endDate,
			String lenderName, long applyId) {

		return informationAssuranceDao.totalInfoAss(supervisorId, applyName,
				status, startDate, endDate, lenderName, applyId);

	}
	
	/**
	 * 查询 信用额度申请记录
	 * 
	 * @param shareId
	 *            保证人ID
	 * @return
	 * @author fanghanyong
	 * @createDate 2018年01月11日
	 */
	public InformationAssuranceBean pageOfInfoAss(long shareId) {

		return informationAssuranceDao.pageOfInfoAss(shareId);
	}

	/**
	 *	后台--风控 -- 保证人承诺函展示
	 * @param currPage
	 * @param pageSize
	 * @param drawee
	 * @param showType
	 * @param orderValue
	 * @param lender_name
	 * @return
	 * @author Liangqiongfeng
	 *@version 2018-3-2 
	 */
	public PageBean<InformationAssuranceBean> pageOfInfoAss(int currPage,
			int pageSize, String drawee, int showType, int orderValue,
			String lender_name) {
		
		return informationAssuranceDao.pageOfInfoAss(currPage, pageSize,drawee, showType,orderValue, lender_name);
	}

	/**
	 *	查询待签署保证金
	 * @param lender_id 放款id
	 * @param user_id 申请用户id
	 * @return
	 * @author xiaojun
	 *@version 2018-4-14 
	 */
	public List<InformationAssuranceBean> findList(long lender_id, long user_id) {
		
		return informationAssuranceDao.findList(lender_id,user_id);
	}

	
}
